package ch03.jianDa.servlet;

import ch03.jianDa.dao.Action;
import ch03.jianDa.factory.ActionFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

@WebServlet(urlPatterns = "/MainServlet")
public class MainServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");//设置请求字符集
        response.setContentType("text/html");//设置响应字符集

        //请求网页资源
        String key = request.getParameter("key");
        //利用key去读取properties文件
        Properties properties =new Properties();
        properties.load(new FileInputStream("D:\\T329\\ZY\\Y2\\01\\ch01\\Reflection\\src\\ch03\\jianDa\\Action.properties"));
        String keyValue =properties.get(key).toString();

        try {

            Action dao = ActionFactory.getInstance(keyValue);
            dao.execute(request,response);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
